package com.example.demo.client.view;

import com.example.demo.client.common.CommonRequest;
import com.example.demo.client.common.ViewKey;
import com.example.demo.ds.MyHashMap;
import com.example.demo.ds.MyList;
import com.example.demo.server.common.*;
import com.example.demo.server.controller.ArticleController;
import com.example.demo.server.model.ArticleInfo;
import com.example.demo.server.model.UserInfo;


public class MyListView implements View {

    ArticleController articleController = new ArticleController();

    /**
     *  展示页面
     */
    public void showPage(UserInfo userInfo, MyList<ArticleInfo> myList) {
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("+--------------------------------------------------------------------------------+");
        System.out.printf(" %s [%s]    主页（list） 写博客（write）  退出登录（logout）         \n", APPUtils.now(), userInfo.getName());
        System.out.printf("+---[sum:%03d]--------------------------------------------------------------------+\n", myList.size());
        if(myList.size() == 0) {
            System.out.println("|                                                                                |");
            System.out.println("| Nothing...                                                                     |");
            System.out.println("|                                                                                |");
        }
        for (int i = 0; i < myList.size(); i++) {
            ArticleUtils.printBriefBlog(myList.get(i), i + 1);
        }
        System.out.println("|________________________________________________________________________________|");
    }


    @Override
    public CommonRequest jump(CommonRequest request) {
        UserInfo userInfo = SessionUtils.getUser();
        if(userInfo == null) {
            APPUtils.printPopUp("Please Login!");
            return CommonRequest.createRequest(ViewKey.LOGIN);
        }
        CommonResponse commonResponse = articleController.getMyArts(userInfo);
        MyList<ArticleInfo> myList = (MyList<ArticleInfo>) commonResponse.getData();
        showPage(userInfo, myList);

        //输入命令
        String command = APPUtils.input();
        // 1. 两个值 / 一个值
        MyList<String> handledCommand = APPUtils.handleCommand(command);
        if(handledCommand.size() == 2 && handledCommand.get(1).matches("^[1-9]\\d*$")) {
            int index = Integer.parseInt(handledCommand.get(1));
            //命令错误
            if(index > myList.size()) {
                APPUtils.printPopUp("ERROR: Please Again");
                return CommonRequest.createRequest(ViewKey.MY_LIST);
            }
            //命令成功
            String opera = handledCommand.get(0);
            switch (opera) {
                case ViewKey.DETAIL:
                    return CommonRequest.createRequest(ViewKey.DETAIL, myList.get(index - 1));
                case ViewKey.UPDATE:
                    MyHashMap<String, Object> map = new MyHashMap<>();
                    map.put("submit", false);
                    map.put("art", myList.get(index - 1));
                    return CommonRequest.createRequest(ViewKey.UPDATE, map);
                case ViewKey.DELETE:
                    articleController.delete(myList.get(index - 1));
                    return CommonRequest.createRequest(ViewKey.MY_LIST);
                default:
                    APPUtils.printPopUp("ERROR: Please Again");
                    return CommonRequest.createRequest(ViewKey.MY_LIST);
            }
        } else if(handledCommand.size() == 1) {
            String view = handledCommand.get(0);
            CommonRequest commonRequest = APPUtils.checkJump(view,
                    ViewKey.LOGOUT, ViewKey.MY_LIST, ViewKey.LIST, ViewKey.WRITE);
            if(commonRequest != null) {
                return commonRequest;
            }
        }
        APPUtils.printPopUp("ERROR: Please Again");
        return CommonRequest.createRequest(ViewKey.MY_LIST);
    }
}
